import json

from django.shortcuts import render, redirect
from django.http import HttpResponse

# Create your views here.
from django.urls import reverse
from django.views.generic.base import View


def index(req):
    url = reverse('book:index')
    print(url)
    print(req)
    return render(req, "book/index.html")
    # return render(req, url)
    return HttpResponse("hahah")


# #################################HttpRequest对象##############################
# 位置参数
def paramsA(req, bookid, cateid):
    print(req, cateid, bookid)
    return HttpResponse("位置参数")


# 关键字参数
def paramsB(req, bookid, cateid):
    print(req, cateid, bookid)
    return HttpResponse("关键字参数")


# QueryDict对象
def paramsC(req):
    print(req)
    parms = req.GET
    print(parms)
    name = parms.getlist("username")
    password = parms.get("password")
    print(name)
    print(password)
    return HttpResponse("QueryDict对象")


# 请求体fromdata格式
def paramsD(req):
    print(req.POST)
    params = req.POST
    uaername = params.get("username")
    password = params.get("password")
    print(uaername)
    print(password)
    return HttpResponse("请求体fromdata格式")


# 请求体JSON格式
def paramsE(req):
    print(req.body)
    params = json.loads(req.body.decode())
    print(params)
    uaername = params.get("username")
    password = params.get("password")
    print(uaername)
    print(password)
    return HttpResponse("请求体JSON格式")


# #################################HttpResponse对象##############################
def response(req):
    url = reverse('book:index')
    print(url)

    # 自定义响应头
    setResponse = HttpResponse("HttpResponse对象")
    print(setResponse)
    # 自定义响应头
    setResponse["name"] = "python"
    setResponse.status_code = 400
    # return render(req, "book/index.html")
    return setResponse


# #################################redirect重定向##############################
def responseRedirect(request):
    print("book/index/100/50")
    url = "/paramsA/100/50"
    return redirect(url)
    # return HttpResponse("4444")


# #################################设置cookie##############################
def setCookie(request):
    response = HttpResponse('ok')
    response.set_cookie('feiyuCOokie', 'python1')  # 临时cookie
    response.set_cookie('feiyuCOokie2', 'python2', max_age=30)  # 有效期一小时
    return response


# #################################获取cookie##############################
def getCookie(request):
    cookie = request.COOKIES.get("feiyuCOokie")
    print(cookie)
    str = "设置的cookie为：" + cookie
    response = HttpResponse(str)
    response.set_cookie('feiyuCOokie', cookie)
    return response

    return HttpResponse(str)


# #################################设置session##############################
def setSession(req):
    req.session["feiyusession"] = "hahahahah66666666666"
    req.session["name"] = "hahahahah66666666666"
    session = HttpResponse("设置session")
    return session


# #################################获取session##############################
def getSession(request):
    session = request.session.get("feiyusession")
    session2 = request.session.get("name")
    print(session)
    str = "设置的cookie为：" + session + "-------------------------------" + session2
    response = HttpResponse(str)
    # response.set_cookie('feiyuCOokie', cookie)
    return response

    return HttpResponse(str)


# #################################类视图##############################
class RegisterView(View):
    def get(self, req):
        return HttpResponse("get")

    def post(self, req):
        return HttpResponse("post")


"""
个人中心页面      --  必须登陆才能显示
GET 方式 展示 个人中心
POST 实现个人中心信息的修改
定义类视图
"""
from django.contrib.auth.mixins import LoginRequiredMixin


class CenterView(LoginRequiredMixin, View):

    def get(self, request):
        return HttpResponse('个人中心展示')

    def post(self, request):
        return HttpResponse('个人中心修改')


class tenpletaIndex(View):
    def get(self, request):
        context = {
            'city': '北京',
            'adict': {
                'name': '西游记',
                'author': '吴承恩'
            },
            'alist': [1, 2, 3, 4, 5]
        }
        return render(request, 'book/index.html', context)

    def post(self, request):
        return HttpResponse('个人中心修改')
